﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Moon.Sql
{
    /// <summary>
    /// mysql 特有功能 通过扩展实现
    /// </summary>
    public static class MySqlExtention
    {
        /// <summary>
        /// mysql特有功能 忽略重复键
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="insertable"></param>
        /// <returns></returns>
        public static IInsertable<T> MySqlIgnoreInto<T>(this IInsertable<T> insertable) where T : class
        {
            if (!(insertable is MySqlInsertable<T>))
            {
                throw new Exception("mysql specific functions");
            }
            (insertable as MySqlInsertable<T>).IsIgnoreInto = true;
            return insertable;
        }

        public static IInsertable<T> MySqlReplaceInto<T>(this IInsertable<T> insertable) where T : class
        {
            if (!(insertable is MySqlInsertable<T>))
            {
                throw new Exception("mysql specific functions");
            }
            (insertable as MySqlInsertable<T>).IsReplaceInto = true;
            return insertable;
        }

        public static IInsertable<T> MySqlOnDuplicateKeyUpdate<T>(this IInsertable<T> insertable) where T : class
        {
            if (!(insertable is MySqlInsertable<T>))
            {
                throw new Exception("mysql specific functions");
            }
            (insertable as MySqlInsertable<T>).IsOnDuplicateKeyUpdate = true;
            return insertable;
        }

        public static int MySqlBulkInsert<T>(this IInsertable<T> insertable, string csvFilePath)
        {
            if (!(insertable is MySqlInsertable<T>))
            {
                throw new Exception("mysql specific functions");
            }
            if (string.IsNullOrWhiteSpace(csvFilePath))
            {
                throw new Exception("the parameter is null");
            }
            return (insertable as MySqlInsertable<T>).BulkInsert(csvFilePath);
        }

        public static async Task<int> MySqlBulkInsertAsync<T>(this IInsertable<T> insertable, string csvFilePath)
        {
            if (!(insertable is MySqlInsertable<T>))
            {
                throw new Exception("mysql specific functions");
            }
            if (string.IsNullOrWhiteSpace(csvFilePath))
            {
                throw new Exception("the parameter is null");
            }
            return await (insertable as MySqlInsertable<T>).BulkInsertAsync(csvFilePath);
        }

        public static int MySqlBulkInsert<T>(this IInsertable<T> insertable, DataTable table)
        {
            if (!(insertable is MySqlInsertable<T>))
            {
                throw new Exception("mysql specific functions");
            }
            return (insertable as MySqlInsertable<T>).BulkInsert(table);
        }

        public static async Task<int> MySqlBulkInsertAsync<T>(this IInsertable<T> insertable, DataTable table)
        {
            if (!(insertable is MySqlInsertable<T>))
            {
                throw new Exception("mysql specific functions");
            }
            return await (insertable as MySqlInsertable<T>).BulkInsertAsync(table);
        }
    }
}
